﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Frame.xaml
    /// </summary>
    public partial class Frame : UserControl
    {
        Point lastClick = new Point(-1, -1);
        FrameworkElement content;

        public Frame(double Height, double Width, FrameworkElement content)
        {
            InitializeComponent();
            this.Height = Height + 30;
            this.Width = Width + 30;
            this.ClipToBounds = false;
            ScaleTransform st = new ScaleTransform(1, 1, this.Width / 2, this.Height / 2);
            RotateTransform rt = new RotateTransform(0, this.Width / 2, this.Height / 2);
            TranslateTransform tt = new TranslateTransform();
            SkewTransform sKt = new SkewTransform();
            TransformGroup transGroup = new TransformGroup();
            transGroup.Children.Add(st);
            transGroup.Children.Add(rt);
            transGroup.Children.Add(tt);
            transGroup.Children.Add(sKt);
            this.RenderTransform = transGroup;
            this.content = content;
        }

        public ScaleTransform getScale()
        {
            return (ScaleTransform)((TransformGroup)this.RenderTransform).Children[0];
        }

        public ScaleTransform getContentScale()
        {
            return (ScaleTransform)((TransformGroup)content.RenderTransform).Children[0];
        }

        public ScaleTransform getContentParentScale()
        {
            return (ScaleTransform)((TransformGroup)((Canvas)content.Parent).LayoutTransform).Children[0];
        }

        public RotateTransform getRotate()
        {
            return (RotateTransform)((TransformGroup)this.RenderTransform).Children[1];
        }

        public RotateTransform getContentRotate()
        {
            return (RotateTransform)((TransformGroup)content.RenderTransform).Children[1];
        }

        private void Resize(double xChange, double yChange, bool fixLeft, bool fixTop)
        {
            try
            {
                if (content.Height - yChange >= 0 && content.Width - xChange >= 0)
                {
                    this.Height -= yChange * getContentParentScale().ScaleY;
                    this.Width -= xChange * getContentParentScale().ScaleX;
                    content.Height -= yChange;
                    content.Width -= xChange;
                    if (fixLeft)
                    {
                        Canvas.SetLeft(this, Canvas.GetLeft(this) + xChange * getContentParentScale().ScaleX);
                        Canvas.SetLeft(content, Canvas.GetLeft(content) + xChange);
                    }
                    if (fixTop)
                    {
                        Canvas.SetTop(this, Canvas.GetTop(this) + yChange * getContentParentScale().ScaleY);
                        Canvas.SetTop(content, Canvas.GetTop(content) + yChange);
                    }
                }
            }
            catch { }   
        }

        private void Shape_MouseDown(object sender, MouseButtonEventArgs e)
        {
            lastClick = e.GetPosition((Shape)sender);
            ((Shape)sender).CaptureMouse();
        }

        private void Shape_MouseUp(object sender, MouseButtonEventArgs e)
        {
            ((Shape)sender).ReleaseMouseCapture();
        }

        private void TopLeftCorner_MouseEnter(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.SizeNWSE;
        }

        private void RightTopCorner_MouseEnter(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.SizeNESW;
        }

        private void resizeRectangle_MouseLeave(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Arrow;
        }

        private void Rectangle_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point movePosition = e.GetPosition((Rectangle)sender);
                    double xChange = movePosition.X - lastClick.X;
                    double yChange = movePosition.Y - lastClick.Y;
                    string actualSender = ((Rectangle)sender).Name;
                    switch (actualSender)
                    {
                        case "TopLeftCorner": Resize(xChange, yChange, true, true);
                            break;
                        case "MiddleTopCorner": Resize(0, yChange, false, true);
                            break;
                        case "RightTopCorner": Resize(-xChange, yChange, false, true);
                            break;
                        case "MiddleLeftCorner": Resize(xChange, 0, true, false);
                            break;
                        case "BottomMiddleCorner": Resize(0, -yChange, false, false);
                            break;
                        case "BottomRightCorner": Resize(-xChange, -yChange, false, false);
                            break;
                        case "RightMiddleCorner": Resize(-xChange, 0, false, false);
                            break;
                        case "BottomLeftCorner": Resize(xChange, -yChange, true, false);
                            break;
                        default:
                            break;
                    }
                }
            }
            catch { }
        }

        private void RotationElipse_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    Point movePosition = e.GetPosition(this);
                    Point frameCenter = new Point(this.Width / 2 - 10, this.Height / 2 - 10);
                    Point contentCenter = new Point(content.Width / 2, content.Height / 2);
                    double a = lastClick.X - frameCenter.X;
                    double b = lastClick.Y - frameCenter.Y;
                    double c = movePosition.X - frameCenter.X;
                    double d = movePosition.Y - frameCenter.Y;
                    double atanA = Math.Atan2(a, b);
                    double atanB = Math.Atan2(c, d);
                    getRotate().CenterX = frameCenter.X;
                    getRotate().CenterY = frameCenter.Y;
                    getContentRotate().CenterX = contentCenter.X;
                    getContentRotate().CenterY = contentCenter.Y;
                    getRotate().Angle += (atanA - atanB) * 180 / Math.PI - 180;
                    getContentRotate().Angle += (atanA - atanB) * 180 / Math.PI - 180;
                }
            }
            catch { }
        }

        private void MiddleTopCorner_MouseEnter(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.SizeNS;
        }

        private void MiddleLeftCorner_MouseEnter(object sender, MouseEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.SizeWE;
        }

        private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            lastClick = this.RenderTransform.Transform(e.GetPosition((Canvas)sender));
        }

        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.LeftButton == MouseButtonState.Pressed &&
                        (bool)((MainWindow)Window.GetWindow(this)).MoveImgTool.IsChecked)
                {
                    if (lastClick.X == -1)
                    {
                        lastClick = this.RenderTransform.Transform(e.GetPosition((Canvas)sender));
                    }
                    Point movePosition = this.RenderTransform.Transform(e.GetPosition((Canvas)sender));
                    double xChange = movePosition.X - lastClick.X;
                    double yChange = movePosition.Y - lastClick.Y;
                    Canvas.SetTop(this, Canvas.GetTop(this) + yChange * getContentParentScale().ScaleY);
                    Canvas.SetLeft(this, Canvas.GetLeft(this) + xChange * getContentParentScale().ScaleX);
                    Canvas.SetLeft(content, Canvas.GetLeft(content) + xChange);
                    Canvas.SetTop(content, Canvas.GetTop(content) + yChange);
                }
            }
            catch { }
        }

        

        


    }
}
